import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    will() {

    }

    @o orderList = [];
    @o orderTotal = 0;
    @o currentPage = 1;
    @o startTime = '';
    @o endTime = '';
    @o phone = '';
    @o nickName = '';
    @o recordStartTime = '';
    @o recordEndTime  = '';
    @o recordPhone = '';
    @o recordName = '';
    @o downUrl = '';
    @o firstRequest = false;

    @a
    paginationChange(current) {
        this.currentPage = current;
        this.getOrderList(true);
    }

    @o
    setToDefault(){
        this.orderList = [];
        this.orderTotal = 0;
        this.currentPage = 1;
        this.startTime = '';
        this.endTime = '';
        this.phone = '';
        this.nickName = '';
        this.recordStartTime = '';
        this.recordEndTime  = '';
        this.recordPhone = '';
        this.recordName = '';
        this.downUrl = '';
        this.firstRequest = false;
    }

    @a
    startTimeChange(e) {
        this.startTime = e._d && e._d.getTime();
    }

    @a
    endTimeChange(e) {
        this.endTime = e._d && e._d.getTime();
    }

    @a
    clearStartTime() {
        this.startTime = '';
    }

    @a
    clearEndTime() {
        this.endTime = '';
    }
    nameChange(v){
        this.nickName = v || '';
    }
    closeNameHandle(){
        this.nickName = '';
        this.search()
    }
    phoneChange(v){
        this.phone = v || '';
    }
    closePhoneHandle(){
        this.phone = '';
        this.search()
    }

    searchTime(){
        if (!this.startTime && !this.endTime) {
            this.search()
        }
        this.search()
    }

    downloadExcel() {
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips:'确定导出当前所选画展数据吗？',
            func: () => {
                request({
                    url: $$.api('getAllRecords'),
                    method: 'post',
                    data: {
                        name: this.recordName || null,
                        phone: this.recordPhone || null,
                        beginTime:this.recordStartTime || null,
                        endTime:this.recordEndTime || null,
                        export:'true'
                    }
                }).then(res => {
                    this.stores.Message.showMessage(1, '导出成功');
                    this.downUrl = res.data.url;
                    let a = document.createElement('a');
                    a.href = 'http://' + this.downUrl;
                    a.download = '订单数据';
                    a.click();
                }).catch(err => {
                    this.stores.Message.fire('message', {
                        type: $$.SEM.ERROR_TEXT,
                        text: err.message
                    });
                })
            }
        });
    }


    @a
    nameInput(e) {
        if (e.which === 13) {
            this.search();
        }
    }

    @a
    search() {
        // if (!this.startTime && this.endTime) {
        //     this.stores.Message.fire('message', {type: $$.SEM.WARNING_TEXT, text: '开始时间不能为空'});
        //     return;
        // }
        // if (this.startTime && !this.endTime) {
        //     this.stores.Message.fire('message', {type: $$.SEM.WARNING_TEXT, text: '结束时间不能为空'});
        //     return;
        // }
        if (!!this.startTime && !!this.endTime && this.startTime > this.endTime) {
            this.stores.Message.fire('message', {
                type: $$.SEM.WARNING_TEXT,
                text: '开始时间不能大于结束时间'
            });
            return;
        }
        this.currentPage = 1;
        this.recordStartTime= this.startTime;
        this.recordEndTime  = this.endTime;
        this.recordPhone = this.phone;
        this.recordName = this.nickName;
        this.getOrderList(true);
    }

    @o
    getOrderList(loading,download) {
        loading && this.stores.Frame.showLoading();
        request({
            url: $$.api('getAllRecords'),
            method: 'post',
            data: {
                pageNumber: this.currentPage,
                pageSize: $$.SEM.ORDER_LIST_SIZE,
                name: this.recordName || null,
                phone: this.recordPhone || null,
                beginTime:this.recordStartTime || null,
                endTime:this.recordEndTime || null,
                export:download || null,
            }
        }).then(res => {
            this.firstRequest = true;
            this.stores.Frame.hideLoading();
            this.orderList = res.data.list;
            this.orderTotal = res.data.total;
        }).catch(err => {
            this.stores.Message.fire('message', {
                type: $$.SEM.ERROR_TEXT,
                text: err.message
            });
        })
    }

    mount() {
        this.getOrderList()
    }

}

export default Store;